package arithmetic.LeetCode;

import java.util.Arrays;
import java.util.List;

import com.google.common.collect.Lists;

/**
 * 539. 最小时间差
 * 给定一个 24 小时制（小时:分钟 "HH:MM"）的时间列表，找出列表中任意两个时间的最小时间差并以分钟数表示。
 *
 * 示例 1：
 * 输入：timePoints = ["23:59","00:00"]
 * 输出：1
 *
 * 示例 2：
 * 输入：timePoints = ["00:00","23:59","00:00"]
 * 输出：0
 *
 * https://leetcode.cn/problems/minimum-time-difference/
 * @author jiangfeng on 2023/6/11
 */
public class FindMinDifference {
    public static void main(String[] args) {
        //System.out.println(new Solution().findMinDifference(Lists.newArrayList("23:59","00:00")));
        System.out.println(new Solution().findMinDifference(Lists.newArrayList("00:00","23:59","00:00")));
    }
    static class Solution {
        public int findMinDifference(List<String> timePoints) {
            int[] time = new int[timePoints.size()];
            for(int i=0;i<timePoints.size();i++){
                String[] a = timePoints.get(i).split(":");
                time[i] = Integer.parseInt(a[0])*60+Integer.parseInt(a[1]);
            }
            // 排个序
            Arrays.sort(time);
            int min = time[0]+24*60-time[time.length-1];
            for(int i=1;i<time.length;i++){
                min=Math.min(min,time[i]-time[i-1]);
            }
            return min;

        }
    }
}
